#include "msgwidget.h"
#include "ui_msgwidget.h"



MsgWidget::MsgWidget(QWidget* parent)
    : QWidget(parent), m_hovered(false), m_selected(false),
      ui(new Ui::MsgWidget)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground, true);
    setMouseTracking(true);
}
MsgWidget::~MsgWidget()
{
    delete ui;
}

void MsgWidget::setHovered(bool hovered) {
    m_hovered = hovered;
    update();
}

void MsgWidget::setSelected(bool selected) {
    m_selected = selected;
    update();
}

void MsgWidget::enterEvent(QEvent* event) {
    setHovered(true);
    QWidget::enterEvent(event);
}

void MsgWidget::leaveEvent(QEvent* event) {
    setHovered(false);
    QWidget::leaveEvent(event);
}

void MsgWidget::mousePressEvent(QMouseEvent* event) {
    setSelected(true);
    QWidget::mousePressEvent(event);
}

void MsgWidget::paintEvent(QPaintEvent* event) {
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制背景色
    QColor bgColor = m_selected ? QColor(0, 120, 215) : m_hovered ? QColor(225, 225, 225) : Qt::white;
    painter.fillRect(rect(), bgColor);

//    // 绘制文本
//    QString text = "This is a message widget";
//    painter.setPen(Qt::black);
//    painter.drawText(rect(), Qt::AlignCenter, text);
}
